<?php

namespace App\HttpController;

use App\Model\Service\BaseService;

abstract class Base extends SuperBase
{
    // 是否需要token(验证登陆身份)
    protected bool $needToken = true;

    protected array $validateSuccessData = [];

    protected ?BaseService $service = null;

    protected function onRequest(?string $action): ?bool
    {

        if ($this->needToken) {

            $token = $this->request()->getHeader('token')[0] ?? '';

            if (!$token) {
                $this->writeJson(401, null, '请先登录');
                return false;
            }

            $isOk = $this->verifyToken($token);

            if (!$isOk) {
                $this->writeJson(401, null, '身份已过期，请重新登陆');
                return false;
            }

        }

        $this->getPostParams();

        $checkValidate = $this->checkValidate();

        if ($checkValidate[0] === 0) {
            $this->writeJson(400, null, $checkValidate[1]);
            return false;
        }

        $this->validateSuccessData = $checkValidate[1];

        $this->initService($this->postParams, $this->loginUser);

        return true;

    }


}